import {
  SEARCH_FOCUS,
  SEARCH_BLUR,
  CHANGE_SEARCH_LIST,
  MOUSE_ENTER,
  MOUSE_LEAVE,
  CHANGE_PAGE_LIST
} from './actionType'
import axios from 'axios'

const changeSearchList = data => ({
  type: CHANGE_SEARCH_LIST,
  data,
  totalPage: Math.ceil(data.length / 10)
})

export const searchFocus = () => ({
  type: SEARCH_FOCUS
})

export const searchBlur = () => ({
  type: SEARCH_BLUR
})

export const mouseEnter = () => ({
  type: MOUSE_ENTER
})

export const mouseLeave = () => ({
  type: MOUSE_LEAVE
})

export const changePageList = (page, totalPage, spinIcon) => {
  const originAngle = spinIcon.style.transform.replace(/[^0-9]/gi, '')
  spinIcon.style.transform = `rotate(${180 + Number(originAngle)}deg)`
  return {
    type: CHANGE_PAGE_LIST,
    page,
    totalPage
  }
}

export const getSearchList = () => {
  return (dispatch, getState) => {
    axios
      .get('/api/headerList.json')
      .then(res => {
        if (res?.data?.success) {
          dispatch(changeSearchList(res.data.data))
        }
      })
      .catch(err => {
        console.error(err)
      })
  }
}
